package com.erpanderp.wam.util;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

public class WAMUtil {
	
	public static String addTime(String time1, String time2){

		long time1InMinutes = Integer.parseInt(time1.substring(0, time1.indexOf(":"))) * 60 + (Integer.parseInt(time1.substring(time1.indexOf(":")+1)));
		long time2InMinutes = Integer.parseInt(time2.substring(0, time2.indexOf(":"))) * 60 + (Integer.parseInt(time2.substring(time2.indexOf(":")+1)));
		
		long total = time1InMinutes + time2InMinutes;
		String totalTime = null;
		
		if (total > 60) {
            int Hour = (int) (total / 60);
            int Mins = (int) (total % 60);
            
            if (Mins == 0){
            	if(String.valueOf(Hour).length() == 1){
            		totalTime = "0" + Hour + ":00";	
            	}else{
            		totalTime = Hour + ":00";	
            	}
            }	
            else{
            	if(String.valueOf(Hour).length() == 1){
            		totalTime = "0" + Hour + ":00";	
            	}else{
            		totalTime = Hour + ":00";	
            	}
            	totalTime = (String.valueOf(Hour).length() == 1 ? ("0" + Hour) : Hour) + ":" + (String.valueOf(Mins).length() == 1 ? "0" + Mins : Mins);
            }
            	
        } else {
        	totalTime = "00:" + total;
        }
        

	     return totalTime;
		
		
	}
	
	/**
	 * Method returns timesheet calculation end date for the current month
	 * @return
	 */
	public Date getTimesheetEndDate(String month){
		Calendar endDate = Calendar.getInstance();
		System.out.println(endDate.get(Calendar.DAY_OF_MONTH));
		System.out.println(endDate.get(Calendar.MONTH));
		Map<String, Integer> monthsMap = CommonLookup.getMonths();
		
		endDate.set(Calendar.MONTH, monthsMap.get(month));
		System.out.println(endDate.get(Calendar.MONTH));
		endDate.set(Calendar.DAY_OF_MONTH, 24);
		endDate.set(Calendar.HOUR_OF_DAY, 0);
		endDate.set(Calendar.MINUTE, 0);
		endDate.set(Calendar.SECOND, 0);
		
		System.out.println(new WAMUtil().getTimesheetStartDate(endDate));
		
		return endDate.getTime();
	}

	/**
	 * Method returns timesheet calculatio start date base on current month end date
	 * @param timesheetEndDate
	 * @return
	 */
	public Date getTimesheetStartDate(Calendar timesheetEndDate){
		Calendar startDate = new GregorianCalendar(timesheetEndDate.get(Calendar.YEAR), timesheetEndDate.get(Calendar.MONTH), 25);
		startDate.add(Calendar.MONTH, -1);
		return startDate.getTime();
	}
	
	public static Map<String, Integer> getLastSixYears(){
		Map<String, Integer> yearsMap = new LinkedHashMap<String, Integer>();
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		for(int i = 6; i >= 1 ; i--){
			yearsMap.put(String.valueOf(calendar.get(Calendar.YEAR)), calendar.get(Calendar.YEAR));
			calendar.add(Calendar.YEAR, -1);
		}
		return yearsMap;
	}
	

	//Converting Number to Words
		
		static String ones[] = { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven","Eight", "Nine"};
		static String tens [] = { "Hundred", "Thousand", "Lakh", "Crore" };
		static String hudreads[] = { "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen","Fifteen", "Sixteen", "Seventeen", "Eighteen", "Ninteen" };
		static String thousands[] = { "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy",     "Eighty", "Ninty" };
		static String words;
		
		public static String NumberToWords (int number){			       
		       
			int n = 1;
	        int word;
	        words = "";
	        while (number != 0) {
                switch (n) {
                case 1:
                        word = number % 100;
                        pass(word);
                        if (number > 100 && number % 100 != 0) {
                                show("and ");
                        }
                        number /= 100;
                        break;
                case 2:
                        word = number % 10;
                        if (word != 0) {
                                show(" ");
                                show(tens[0]);
                                show(" ");
                                pass(word);
                        }
                        number /= 10;
                        break;
                case 3:
                        word = number % 100;
                        if (word != 0) {
                                show(" ");
                                show(tens[1]);
                                show(" ");
                                pass(word);
                        }
                        number /= 100;
                        break;
                case 4:
                        word = number % 100;
                        if (word != 0) {
                                show(" ");
                                show(tens[2]);
                                show(" ");
                                pass(word);
                        }
                        number /= 100;
                        break;
                case 5:
                        word = number % 100;
                        if (word != 0) {
                                show(" ");
                                show(tens[3]);
                                show(" ");
                                pass(word);
                        }
                        number /= 100;
                        break;

                }
                n++;
	        }
	            return words;
	     }
			
		public static void pass(int number) {
	        int word, q;
	        if (number < 10) {
	                show(ones[number]);
	        }
	        if (number > 9 && number < 20) {
	                show(hudreads[number - 10]);
	        }
	        if (number > 19) {
	                word = number % 10;
	                if (word == 0) {
	                        q = number / 10;
	                        show(thousands[q - 2]);
	                } else {
	                        q = number / 10;
	                        show(ones[word]);
	                        show(" ");
	                        show(thousands[q - 2]);
	                }
	         }
	    }
		
		public static void show(String s) {
		        String st;
		        st = words;
		        words = s;
		        words += st;
		}
  
	//Adding Commas to a number
		public static String addCommasToNumber(String number) {
		
		    int i=0;
		    int lenght= 0;
		    lenght = number.length();
		    StringBuffer Amountbuf = new StringBuffer(number);
		    int lnAmount = Integer.parseInt(number);
			if(lnAmount > 999)
		    {
		       for(i=lenght;i>0;i=i-2)
		       {
		         if(i==lenght)
		         {
		           i= i-3;
		           Amountbuf = Amountbuf.insert(i, ",");
		         }else{
		         Amountbuf = Amountbuf.insert(i, ",");
		         }
		       }
		    }
			number = Amountbuf.toString();
		    return number;
		}
		
		public static void main(String a[]){
			System.out.println(WAMUtil.addTime("01:01", "01:01"));
		}
}
